import java.util.HashMap;
import java.util.Map;

public class Solution2006 {
    public int countKDifference(int[] nums, int k) {
        Map<Integer,Integer> map=new HashMap<>();
        int count=0;
        for (int num : nums) {
            count=count+map.getOrDefault(num-k,0)+map.getOrDefault(num+k,0);
            map.put(num,map.getOrDefault(num,0)+1);
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2006().countKDifference(new int[]{1,2,2,1},1));
    }
}
